﻿using Newtonsoft.Json;
using LEDScreenLinkTool.Model;
using LEDScreenLinkTool.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace LEDScreenLinkTool
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            LogHelper.Init();
            LogHelper.Info("======App Start======");
            try
            {
                List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();

                foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
                {
                    if (dictionary.Source != null && IsLanguageResource(dictionary.Source.LocalPath))
                    {
                        dictionaryList.Add(dictionary);
                    }
                }

                ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => LanguageSetting(d.Source.LocalPath, ParamStruct.Lang)); ;
                //将选中语言放到最新位置..
                Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
                //清理过期预览图
                var pngFiles= Directory.GetFiles(Path.Combine(AppContext.BaseDirectory, "Screens"), "*.png");
                foreach (var pngPath in pngFiles)
                {
                    try
                    {
                        File.Delete(pngPath);
                    }
                    catch (Exception fiex)
                    {
                    }
                }
                string settingPath = Path.Combine(AppContext.BaseDirectory, "module.json");
                if (File.Exists(settingPath))
                {
                    string json = File.ReadAllText(settingPath);
                    ModuleOption jsonData = JsonConvert.DeserializeObject<ModuleOption>(json);
                    if (jsonData != null)
                    {
                        //AppInfoInstance.SetTestMode(jsonData.TestMode);
                        AppInfoInstance.SetTestMode(true);
                        AppInfoInstance.SetModuleList(jsonData.Modules);                     
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private bool IsLanguageResource(string localPath)
        {
            var pathName = Path.GetDirectoryName(localPath);
            return pathName.EndsWith("Language");
        }
        private bool LanguageSetting(string localPath, string language)
        {
            var fileName = Path.GetFileName(localPath);
            bool isMatch = false;
            switch (language)
            {
                case "cn":
                    if (fileName.ToLower() == "zh-cn.xaml")
                    {
                        isMatch = true;
                    }
                    break;
                //case "en":
                //    if (fileName.ToLower() == "en-us.xaml")
                //    {
                //        isMatch = true;
                //    }
                //    break;
            }
            return isMatch;
        }
    }
}
